//
//  YBCenter.h
//  YBNetworking
//
//  Created by ybgo on 2018/11/22.
//  Copyright © 2018 嘉联支付有限公司. All rights reserved.
//


/********************************
 ****** 发送请求和管理请求中心 ******
 ********************************/


#import <Foundation/Foundation.h>
#import "YBConst.h"

NS_ASSUME_NONNULL_BEGIN

@class YBConfig, YBEngine;

@interface YBCenter : NSObject

+ (instancetype)center;

/// 单例对象
+ (instancetype)defaultCenter;

/// 如果 YBRequest.server 为'nil' ,且YBRequest.useGeneralServer 为'YES' 会将'generalServer'的值赋值给YBRequest.server
@property (nonatomic, copy, nullable) NSString *generalServer;

/// YBRequestEncryptNormal/YBRequestParamatersTypeDictinary 类型请求参数 如果 YBRequest.useGeneralParameters 为'YES' 且'generalParameters'不为空 会赋值给YBRequest.parameters
@property (nonatomic, strong, nullable, readonly) NSMutableDictionary<NSString *, id> *generalParameters;

/// 如果 YBRequest.useGeneralHeaders 为'YES' 且'generalHeaders'不为空 将赋值给YBRequest.headers
@property (nonatomic, strong, nullable, readonly) NSMutableDictionary<NSString *, NSString *> *generalHeaders;

/// 如果 YBRequest.userInfo 为'nil' 且'generalUserInfo'不为空 将赋值给YBRequest.userInfo
@property (nonatomic, strong, nullable) NSDictionary *generalUserInfo;

/// 默认为'NULL' 请求回调队列 默认使用默认队列
@property (nonatomic, strong, nullable) dispatch_queue_t callbackQueue;

/// 全局请求对象 默认为'[YBEngine sharedEngine]'
@property (nonatomic, strong) YBEngine *engine;

/// 是否在控制台打印请求和回调信息 默认'NO',不打印
@property (nonatomic, assign) BOOL consoleLog;

/** 最大请求数 */
@property (nonatomic, assign) NSInteger operationMaxCount;


#pragma mark - Instance Method

/// 创建 YBConfig对象 配置YBCenter中所的属性
/// @param block 回调
- (void)setupConfig:(void(^)(YBConfig *config))block;

/// 设置请求的block
/// @param block 回调
- (void)setRequestProcessBlock:(YBCenterRequestProcessBlock)block;

/// 设置回调的block
/// @param block 回调
- (void)setResponseProcessBlock:(YBCenterResponseProcessBlock)block;

/// 设置请求错误block
/// @param block 回调
- (void)setErrorProcessBlock:(YBCenterErrorProcessBlock)block;

/// 在YBCenter中设置http请求头，如果该值为'nil' 会移除已经存在的请求头
/// @param value value
/// @param field key
- (void)setGeneralHeaderValue:(nullable NSString *)value forField:(NSString *)field;

/// 在YBCenter中设置公共的请求参数 如果该值为'nil',会移除已经存在的公共请求参数
/// @param value value
/// @param key key
- (void)setGeneralParameterValue:(nullable id)value forKey:(NSString *)key;

#pragma mark - YBRequest Method

/// 创建YBRequest请求 唯一identifier 失败为'nil'
/// @param configBlock <#configBlock description#>
- (nullable NSString *)sendRequest:(YBRequestConfigBlock)configBlock;

/// 创建一个带成功回调的YBRequest请求 唯一identifier 失败为'nil'
/// @param configBlock YBRequest 对象block
/// @param successBlock 成功回调
- (nullable NSString *)sendRequest:(YBRequestConfigBlock)configBlock
                         onSuccess:(nullable YBSuccessBlock)successBlock;

/// 创建一个带失败回调的YBRequest请求 唯一identifier 失败为'nil'
/// @param configBlock YBRequest 对象block
/// @param failureBlock 失败回调
- (nullable NSString *)sendRequest:(YBRequestConfigBlock)configBlock
                         onFailure:(nullable YBFailureBlock)failureBlock;

/// 创建一个带完成回调的YBRequest请求 唯一identifier 失败为'nil'
/// @param configBlock YBRequest 对象block
/// @param finishedBlock 完成回调
- (nullable NSString *)sendRequest:(YBRequestConfigBlock)configBlock
                        onFinished:(nullable YBFinishedBlock)finishedBlock;

/// 创建一个带成功和失败回调的YBRequest请求 唯一identifier 失败为'nil'
/// @param configBlock YBRequest 对象block
/// @param successBlock 成功回调
/// @param failureBlock 失败回调
- (nullable NSString *)sendRequest:(YBRequestConfigBlock)configBlock
                         onSuccess:(nullable YBSuccessBlock)successBlock
                         onFailure:(nullable YBFailureBlock)failureBlock;

/// 创建一个带成功回调、失败回调和完成回调的YBRequest请求 唯一identifier 失败为'nil'
/// @param configBlock YBRequest 对象block
/// @param successBlock 成功回调
/// @param failureBlock 失败回调
/// @param finishedBlock 完成回调
- (nullable NSString *)sendRequest:(YBRequestConfigBlock)configBlock
                         onSuccess:(nullable YBSuccessBlock)successBlock
                         onFailure:(nullable YBFailureBlock)failureBlock
                        onFinished:(nullable YBFinishedBlock)finishedBlock;

/// 创建一个Upload/Download且带进度、成功、失败回调的YBRequest请求 唯一identifier 失败为'nil'
/// @param configBlock YBRequest 对象block
/// @param progressBlock 进度block
/// @param successBlock 成功回调
/// @param failureBlock 失败回调
- (nullable NSString *)sendRequest:(YBRequestConfigBlock)configBlock
                        onProgress:(nullable YBProgressBlock)progressBlock
                         onSuccess:(nullable YBSuccessBlock)successBlock
                         onFailure:(nullable YBFailureBlock)failureBlock;

/// 创建一个Upload/Download且带进度、成功、失败、完成回调的YBRequest请求 唯一identifier 失败为'nil'
/// @param configBlock YBRequest 对象block
/// @param progressBlock 进度block
/// @param successBlock 成功回调
/// @param failureBlock 失败回调
/// @param finishedBlock 完成回调
- (nullable NSString *)sendRequest:(YBRequestConfigBlock)configBlock
                        onProgress:(nullable YBProgressBlock)progressBlock
                         onSuccess:(nullable YBSuccessBlock)successBlock
                         onFailure:(nullable YBFailureBlock)failureBlock
                        onFinished:(nullable YBFinishedBlock)finishedBlock;

#pragma mark - Instance Method to Operate Requests

/// 通过请求的identifier取消正在请求的request
/// @param identifier 请求的唯一标识
- (void)cancelRequest:(NSString *)identifier;

/// 通过请求的identifier取消请求 带取消回调
/// @param identifier 请求唯一标识
/// @param cancelBlock 取消回调
- (void)cancelRequest:(NSString *)identifier
             onCancel:(nullable YBCancelBlock)cancelBlock;

/// 通过请求的identifier获取当前请求
/// @param identifier 请求唯一标识
- (nullable id)getRequest:(NSString *)identifier;

/// 检查当前网络是否可用
- (BOOL)isNetworkReachable;

/// 获取当前网络类型
- (YBNetworkConnectionType)networkConnectionType;

#pragma mark - Class Method

/// 创建 YBConfig对象 配置YBCenter中所的属性
/// @param block <#block description#>
+ (void)setupConfig:(void(^)(YBConfig *config))block;

/// 设置请求的block
/// @param block <#block description#>
+ (void)setRequestProcessBlock:(YBCenterRequestProcessBlock)block;

/// 设置回调的block
/// @param block <#block description#>
+ (void)setResponseProcessBlock:(YBCenterResponseProcessBlock)block;

/// 设置请求错误block
/// @param block <#block description#>
+ (void)setErrorProcessBlock:(YBCenterErrorProcessBlock)block;

/// 在YBCenter中设置http请求头，如果该值为'nil' 会移除已经存在的请求头
/// @param value <#value description#>
/// @param field <#field description#>
+ (void)setGeneralHeaderValue:(nullable NSString *)value forField:(NSString *)field;

/// 在YBCenter中设置公共的请求参数 如果该值为'nil',会移除已经存在的公共请求参数
/// @param value <#value description#>
/// @param key <#key description#>
+ (void)setGeneralParameterValue:(nullable id)value forKey:(NSString *)key;

#pragma mark - YBRequest class Method

+ (nullable NSString *)sendRequest:(YBRequestConfigBlock)configBlock;

+ (nullable NSString *)sendRequest:(YBRequestConfigBlock)configBlock
                         onSuccess:(nullable YBSuccessBlock)successBlock;

+ (nullable NSString *)sendRequest:(YBRequestConfigBlock)configBlock
                         onFailure:(nullable YBFailureBlock)failureBlock;

+ (nullable NSString *)sendRequest:(YBRequestConfigBlock)configBlock
                        onFinished:(nullable YBFinishedBlock)finishedBlock;

+ (nullable NSString *)sendRequest:(YBRequestConfigBlock)configBlock
                         onSuccess:(nullable YBSuccessBlock)successBlock
                         onFailure:(nullable YBFailureBlock)failureBlock;

+ (nullable NSString *)sendRequest:(YBRequestConfigBlock)configBlock
                         onSuccess:(nullable YBSuccessBlock)successBlock
                         onFailure:(nullable YBFailureBlock)failureBlock
                        onFinished:(nullable YBFinishedBlock)finishedBlock;

+ (nullable NSString *)sendRequest:(YBRequestConfigBlock)configBlock
                        onProgress:(nullable YBProgressBlock)progressBlock
                         onSuccess:(nullable YBSuccessBlock)successBlock
                         onFailure:(nullable YBFailureBlock)failureBlock;

+ (nullable NSString *)sendRequest:(YBRequestConfigBlock)configBlock
                        onProgress:(nullable YBProgressBlock)progressBlock
                         onSuccess:(nullable YBSuccessBlock)successBlock
                         onFailure:(nullable YBFailureBlock)failureBlock
                        onFinished:(nullable YBFinishedBlock)finishedBlock;


#pragma mark -

+ (void)cancelRequest:(NSString *)identifier;

+ (void)cancelRequest:(NSString *)identifier
             onCancel:(nullable YBCancelBlock)cancelBlock;

+ (nullable id)getRequest:(NSString *)identifier;

+ (BOOL)isNetworkReachable;

+ (YBNetworkConnectionType)networkConnectionType;

#pragma mark -

+ (void)addSSLPinningURL:(NSString *)url;
+ (void)addSSLPinningCert:(NSData *)cert;
+ (void)addTwowayAuthenticationPKCS12:(NSData *)p12 keyPassword:(NSString *)password;

@end

#pragma mark - YBConfig

@interface YBConfig : NSObject

/// 公共的请求地址
@property (nonatomic, copy, nullable) NSString *generalServer;

/// 公共的请求参数
@property (nonatomic, strong, nullable) NSDictionary<NSString *, id> *generalParameters;

/// 公共的请求头
@property (nonatomic, strong, nullable) NSDictionary<NSString *, NSString *> *generalHeaders;

/// 公共的userInfo
@property (nonatomic, strong, nullable) NSDictionary *generalUserInfo;

/// 请求队列
@property (nonatomic, strong, nullable) dispatch_queue_t callbackQueue;

/// 全局请求engine
@property (nonatomic, strong, nullable) YBEngine *engine;
/// 是否在控制台打印日志
@property (nonatomic, assign) BOOL consoleLog;
/** 最大请求数 */
@property (nonatomic, assign) NSInteger operationMaxCount;

@end

NS_ASSUME_NONNULL_END
